package com.project.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.config.LoginThreadLocal;
import com.project.dto.ConfirmTenderDto;
import com.project.dto.ConfirmTenderGoodsDto;
import com.project.dto.SupplyRefundRecordDto;
import com.project.entity.*;
import com.project.mapper.*;
import com.project.service.ITenderService;
import com.project.util.ConvertBeanUtil;
import com.project.util.SnowIdUtils;
import com.project.vo.AddConfirmGoodsVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class TenderServiceImpl implements ITenderService {

    @Autowired
    private ITenderMapper tenderMapper;

    @Autowired
    private IBiddMapper biddMapper;

    @Autowired
    private IApplyInfoMapper applyInfoMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private IConfirmOrderMapper confirmOrderMapper;

    @Autowired
    private IConfirmGoodsMapper confirmGoodsMapper;
    /**
     * 查找招标书id的所有投标书
     */
    @Override
    public List<TenderEntity> findByBiddRef(String biddRef) {
        QueryWrapper<TenderEntity> qw = new QueryWrapper<>();
        qw.eq("t_biddRef", biddRef);
        return tenderMapper.selectList(qw);
    }


    /**
     * 确定该投标书中标
     */
    //TODO 审核中标 1、添加中标2、添加中标商品3、发供应商
    @Override
    public void confirmTender(String tenderRef,String goodsList) {
        //修改供应商投标书状态
        QueryWrapper<TenderEntity> qw = new QueryWrapper<>();
        qw.eq("t_tenderRef", tenderRef);
        TenderEntity tenderEntity = tenderMapper.selectOne(qw);
        String inviteId = tenderEntity.getInviteId();

        //查找获得招标书的唯一编号，把该招标书对应的所有投标改为未中标
        QueryWrapper<TenderEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t_biddRef", inviteId);
        List<TenderEntity> tenderEntities = tenderMapper.selectList(queryWrapper);
        for (TenderEntity t: tenderEntities) {
            t.setVerifyTime(LocalDateTime.now());
            t.setStatus(2);
            tenderMapper.updateById(t);
        }

        //     该投标书改为中标
        tenderEntity.setStatus(1);
        tenderEntity.setVerifyName(LoginThreadLocal.getLoginVerify().getName());
        tenderEntity.setVerifyTime(LocalDateTime.now());
        tenderMapper.updateById(tenderEntity);//修改


        QueryWrapper<BiddEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("b_biddRef", tenderEntity.getInviteId());
        BiddEntity biddEntity = biddMapper.selectOne(queryWrapper1);
        //添加招标书
        ConfirmOrderEntity confirmOrder=new ConfirmOrderEntity();
        confirmOrder.setConfirmRef(SnowIdUtils.uniqueLong());
        confirmOrder.setTitle(tenderEntity.getTitle());
        confirmOrder.setBiddId(tenderEntity.getInviteId());
        confirmOrder.setSupplierRef(tenderEntity.getSupplierId());
        confirmOrder.setSupplierName(tenderEntity.getSupplierName());
        confirmOrder.setInfo(biddEntity.getInfo());
        confirmOrder.setPact(SnowIdUtils.uniqueLongHex());
        confirmOrderMapper.insert(confirmOrder);



        //转换商品
        List<AddConfirmGoodsVo> goodsVos= JSONObject.parseArray(goodsList, AddConfirmGoodsVo.class);
        //存本地数据库的
        List<ConfirmGoodsEntity> goodsEntities = ConvertBeanUtil.copyList(goodsVos, ConfirmGoodsEntity.class,(m,n)->{
            n.setConfirmRef(confirmOrder.getConfirmRef());
        });
//       加入本地数据库
        for (int i = 0; i < goodsEntities.size(); i++) {
            confirmGoodsMapper.insert(goodsEntities.get(i));
        }


        //发送给供应商的投标书
        ConfirmTenderDto pscTender=new ConfirmTenderDto();
        pscTender.setInviteId(inviteId);
        pscTender.setBiddId(confirmOrder.getBiddId());
        pscTender.setPact(confirmOrder.getPact());
        pscTender.setInfo(tenderEntity.getInfo());
        pscTender.setTitle(confirmOrder.getTitle());
        pscTender.setSupplierId(confirmOrder.getSupplierRef());
        //发给供应商的商品
        List<ConfirmTenderGoodsDto> pscGoods = ConvertBeanUtil.copyList(goodsVos, ConfirmTenderGoodsDto.class, (m, n) -> {
            n.setGoodsName(m.getProductName());
            n.setSum(m.getApplyNumber());
        });
        pscTender.setGoodsDtoList(pscGoods);

        String jsonObject =  JSONObject.toJSONString(pscTender);
        System.err.println(jsonObject);

        rabbitTemplate.convertAndSend("topicExchangeTwo","rabbit.TenderVerifyResult",jsonObject);

    }


    //TODO 展示想要购买的商品
    @Override
    public List<ApplyInfoEntity> findGoods(String biddRef) {
        //查找标书
        QueryWrapper<BiddEntity> qw = new QueryWrapper<>();
        qw.eq("b_biddRef", biddRef);
        BiddEntity biddEntity =biddMapper.selectOne(qw);


        //查找后端采购申请
        QueryWrapper<ApplyInfoEntity> qw1 = new QueryWrapper<>();
        qw1.eq("a_applyRef", biddEntity.getApplyRef());
        List<ApplyInfoEntity> applyInfoEntityList = applyInfoMapper.selectList(qw1);

        return applyInfoEntityList;
    }


}
